<template>
    <page-header-wrapper>
        <a-card :bordered="false">
            <div class="table-page-search-wrapper">
                <i-filter :members="members" :loading="loading" @query="query"/>
                <s-table :loading="loading" :cols="cols" :data="data" :actions="acts"
                         :operators="operators" :pagination="pagination" @change="change"/>
                <a-form :title="modal.title" :visible="modal.visible" :members="members"
                        :loading="modal.loading" :model="modal.model"
                        @ok="addOk" @cancel="modal.visible = modal.loading = false"/>
            </div>
        </a-card>
    </page-header-wrapper>
</template>

<script>
    import STable from '@/components/Table/PagerTable'
    import IFilter from '@/components/Filter/InterviewFilter'
    import API from "@/api"
    import {Modal} from "ant-design-vue"
    import Adapter from "@/utils/adapter"
    import AForm from './modal/AddForm'

    export default {
        components: {
            STable,
            IFilter,
            AForm
        },
        data() {
            return {
                memberMap: {},
                members: [],

                loading: true,
                data: [],
                cols: [{title: '编号', key: 'id'}, {title: '面试地点', key: 'arrangedPlace'},
                    {title: '面试时间', key: 'arrangedTime'},
                    {title: '创建者', key: 'handlerId'}, {title: '创建时间', key: 'createTime'}],
                acts: [{func: this.view, title: '简历'}, {func: this.edit, title: '编辑'}, {func: this.del, title: '删除'}],
                operators: [{type: 'primary', icon: 'plus', title: '新建', selection: false, func: this.add}],
                pagination: {
                    total: 0,
                    current: 1,
                    pageSize: 10,
                    showSizeChanger: true,
                    showQuickJumper: true
                },
                queryParams: {
                    limit: 10,
                    offset: 0
                },
                modal: {
                    title: '',
                    visible: false,
                    loading: false,
                    model: {}
                }
            }
        },
        mounted() {
            API.User.List().then((res) => {
                if (res) {
                    this.members = res.data || []
                    this.members = this.members.filter((item) => {return item.status !== 0 && item.status !== 4})
                    this.memberMap = {}
                    this.members.forEach((item) => this.memberMap[item.id] = item.name)
                    this.query()
                }
            })
        },
        methods: {
            change() {
                this.queryParams.limit = this.pagination.pageSize
                this.queryParams.offset = (this.pagination.current - 1) * this.pagination.pageSize
                this.query()
            },
            query(params) {
                this.loading = true
                if (params) {
                    params.limit = this.queryParams.limit
                    params.offset = this.queryParams.offset
                    this.queryParams = params
                }
                API.Interview.List(this.queryParams).then((res) => {
                    if (res) {
                        this.pagination.total = res.total
                        Adapter.Interview(res.data, this.memberMap)
                        this.data = res.data
                    }
                }).finally(() => this.loading = false)
            },
            view(record) {
                this.$router.push({name: 'interviewDetails', params: {id: record.id, back: '/system/interview'}})
            },
            add() {
                this.modal.title = '添加面试'
                this.modal.model = null
                this.modal.visible = true
            },
            edit(record) {
                this.modal.title = '编辑面试'
                this.modal.model = record
                this.modal.visible = true
            },
            del(record) {
                Modal.confirm({
                    title: '提示',
                    content: '是否确认该面试？',
                    onOk: () => {
                        API.Interview.Del({id: record.id}).then((res) => {
                            if (res) {
                                this.$notification.success({message: '提示', description: '面试已删除'})
                                this.data = this.data.filter(item => item.id !== record.id)
                            }
                        })
                    },
                    onCancel () {}
                })
            },
            addOk(params) {
                this.modal.loading = true
                params.time.second(0)
                params.time.year(params.date.year())
                params.time.month(params.date.month())
                params.time.date(params.date.date())
                params.arrangedTime = params.time.valueOf()
                if (params.id) {
                    API.Interview.Edit(params).then((data) => this.addProcessor(data, params))
                        .finally(() => this.modal.loading = false)
                }
                else {
                    API.Interview.Add(params).then((data) => this.addProcessor(data, params))
                        .finally(() => this.modal.loading = false)
                }
            },
            addProcessor(res, params) {
                if (res) {
                    this.modal.visible = false
                    this.$notification.success({message: '提示', description: params.id ? '修改成功' : '添加成功'})
                    this.query()
                }
            }
        }
    }
</script>
